package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneCommonService;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.service.util.ProdCateUtil;
import com.iwhalecloud.bss.kite.dataservice.cache.OfferCache;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.util.ContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.Optional;


/**
 * 互联网专线产品无需核查原因
 *
 */
@Service
public class LocalNocheckCauseHandler extends AbstractAttrHandler {
    @Autowired
    private IQuerySceneInstService querySceneInstService;
    @Autowired
    private IOperateSceneCommonService operateSceneCommonService;

    @Override
    public void init(CommonAttrVO commonAttr) {
        String sceneInstId = KiteWebContext.getContext().getCurrentSceneInstId();
        String opreatorProvinceNbr= ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr();
        KiteOfferInst mainOfferInst =  querySceneInstService.getMainOfferInst(sceneInstId);
        String specId = Optional.ofNullable(mainOfferInst).map(m -> m.getSpecId()).orElse(null);
        String cataId= null;
        if(!Objects.isNull(specId)){
            cataId= OfferCache.get(specId).getCateId();
        }
        if(KiteStringUtils.isNotEmpty(opreatorProvinceNbr) && KiteStringUtils.equals(opreatorProvinceNbr,"18")){
            if(KiteStringUtils.isNotEmpty(cataId) && KiteStringUtils.equals(cataId, ProdCateUtil.getSingleCateId(ProdCateUtil.DIA_CODE))){
                commonAttr.setShowType("D");
            }
        } else if (KiteStringUtils.equals(LocalKeyConsts.PROVINCE_SHANGHAI, opreatorProvinceNbr)) {
            // 上海以太网 开通方式为“无需核查直开”时，无需核查原因联动展示
            KiteAttr openWayAttr = querySceneInstService.getCommonAttr(sceneInstId, "ORD10005"); // 开通方式
            if (Objects.nonNull(openWayAttr) && KiteStringUtils.equals("0", openWayAttr.getValue())) {
                if (KiteStringUtils.equals(cataId, ProdCateUtil.getSingleCateId(ProdCateUtil.YTW_CODE))) {
                    commonAttr.setShowType("D");
                } else if (KiteStringUtils.equals(cataId, ProdCateUtil.getSingleCateId(ProdCateUtil.MV_CODE))){
                    commonAttr.setShowType("M");
                }
            }
        }
        setCommonAttr(commonAttr);
    }
}
